package com.imay.live.helper;

import android.content.Context;

import com.imay.base.util.StringUtil;
import com.imay.live.activity.MainActivity2;
import com.imay.live.activity.load.StartActivity;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoSocketFactory;
import com.imay.live.BuildConfig;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

/**
 * Created by jeaman on 2017/5/24.
 */

public class UmengPushHelper {
    private static UmengPushHelper instance;
    private String mDeviceToken;
    public static final String NOTIFY_ENTER_ROOM = "enter_room";
    public static final String ENTER_USER_CALLLIVE = "enter_user_callLive";

    public synchronized static UmengPushHelper getInstance() {
        if (instance == null) {
            instance = new UmengPushHelper();
        }
        return instance;
    }

    public void init(Context context) {
        PushAgent mPushAgent = PushAgent.getInstance(context);
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                if (NOTIFY_ENTER_ROOM.equals(msg.custom)){
                    String roomId = msg.extra.get("roomId");
                    if (!StringUtil.isEmpty(roomId)){//直播间
                        MainActivity2.startLiveActivityForUmengPush(context,"imay://com.imay.live/openwith?type=1&roomId="+roomId);
                    }
                }else if(ENTER_USER_CALLLIVE.equals(msg.custom)){//催直播
                    MainActivity2.startActivityForUmengPush(context,ENTER_USER_CALLLIVE);
                }else{//直接打开应用
                    StartActivity.startForUmengPush(context);
                }
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
        mPushAgent.setDebugMode(!BuildConfig.IS_RELEASE);
//        mPushAgent.setPushIntentServiceClass(PushIntentService.class);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                mDeviceToken = deviceToken;
                //通知服务器
                ProtoRequestHelper.task(ProtoSocketFactory.requestAccountReportDelay()).request(null);
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });
    }

    public String getDeviceToken() {
        return mDeviceToken;
    }


    private void clearNotify(){
    }


}
